<?php
	require_once( "init.php" );
        itemClass::listaItemsSubCat(1);
        //itemClass::getItemsByIDSubcategory(1);
        itemClass::getItemById(1);
	/* ------------------------------GESTION GET --------------------------- */
	$pag = "index";
        //--------------Gestion P1
	if( isset( $_REQUEST['p1'] ) && $_REQUEST['p1'] <> "index" ){//Si existe la variable $_REQUEST['p1']
		$categoria = $_REQUEST['p1'];
                //controlamos que exista la categoria
                $variable = Categoria::existCategory( $categoria );
		if( $variable ){
                    $pag = "subcategorias"; }
                    //echo "La categoria existe? true"; }
		else{
			$pag="404";
		}
                
                //--------------------gestion P2
		if( isset( $_REQUEST['p2'] ) && $pag <> "404" ){//Si existe la variable $_REQUEST['p2']
			$subCategory = $_REQUEST['p2'];
            //comprobamos que exista la categoria
			$variable2 = Subcategoria::existSubCategory( $subCategory );
			if( $variable2 ){
                $pag="listaitems";
				//echo "La scategoria existe? true";
			}
			else{
				$pag="404";
			}
                //------------------gestion p3
			if( isset( $_REQUEST['p3'] ) && $pag <> "404" ){//Si existe la variable $_REQUEST['p3']
				$item = $_REQUEST['p3'];
				//comprobamos que existe el tiem
				$variable3 = itemClass::existItem( $item );
				if( $variable3 ){
					$pag = "item";
				}
				else{
					$pag = "404";
				}
			}
		}
	}

	/* ------------------------------ CASOS (switch) ------------------------- */

	switch( $pag ){
		case "index" : 
			$page = new viewClass();
			$page->setTypepage( $pag );
			$page->setDescription('index');
			$page->setLeftmenu( Categoria::getCategorias());
			$page->setKeys($_SESSION['site']->getKeys());
			$page->show();
			break;
		case "subcategorias": 
			$page = new viewClass();
			$page->setNamepage( $categoria );
			$page->setInfopage( $categoria );
			$page->setTypepage( $pag );
			$page->setDescription('sub');
			$page->setLeftmenu( Categoria::getCategorias());
                        $page->setItems ( Subcategoria::getSubCategoriasById(Categoria::getCategoryId( $categoria )) );
			$page->setKeys($_SESSION['site']->getKeys());
			$page->show();
			break;
		 case "listaitems":
			$page = new viewClass();
			$page->setNamepage( $subCategory );
			$page->setTypepage( $pag );
			$page->setDescription('lista');
			$page->setLeftmenu( Categoria::getCategorias() );
			$page->setItems( itemClass::listaItemsSubCat( Subcategoria::getSubCategoryId( $subCategory ) ) );
			$page->setKeys($_SESSION['site']->getKeys());
			$page->show();
			break;
		 case "item":
			$page = new viewClass();
			$page->setTypepage( $pag );
			$page->setDescription('holas');
			$page->setLeftmenu( Categoria::getCategorias());
			$page->setKeys( $_SESSION['site']->getKeys() );
			$page->show();
			break;  
		case "404" :
            echo "Error"; 
			break;
		default : 
			/*$variable = Categoria::existCategory( $categoria );
			if( $variable ){ echo "La categoria existe? true"; }
			else{ echo "La categoria existe? false"; }*/
		break;
	}
?>
